int t=0;
char ten[100];

void add_student()
{
	int i; char key;
	do {		
		printf("\n > Add New Student");
		printf("\n ------------------------------ ");
   lbl :printf("\n   Roll No: "); fflush(stdin); gets(stu[scount].rollno); strUpper(stu[scount].rollno);
	   if (isRollno(stu[scount].rollno)==0)
	 	{
	 		printf("\n The rollno is not valid");	goto lbl;
	 	}		
		if (isRollnoExits(stu[scount].rollno)>=0)
	 	{
	 		printf("\n The class name is already exits"); goto lbl;
	 	}		
		printf("\n Full Name: "); fflush(stdin); gets(stu[scount].name);
		printf("\n  Birthday: "); fflush(stdin); gets(stu[scount].birth);
		printf("\n   Address: "); fflush(stdin); gets(stu[scount].add);
		printf("\n     Class: "); fflush(stdin); gets(stu[scount].classname); strUpper(stu[scount].classname);
		scount++;
		printf("\n ------------------------------ ");
		printf("\n     Do you want to save (y/n): ");	fflush(stdin); scanf("%c",&key); if (key=='n')	scount--;
		printf("\n Do you want to continue (y/n): ");	fflush(stdin); scanf("%c",&key); 
	} while(key!='n');
}

void edit_student(int index=-1)
{
	int i; char key;
	char rollno[10]; struct_student stuTemp;

	do 
	{	
		printf("\n > Edit Student Information");
		if (index>=0)
		{
			i=index; index=-1;
		}
		else 
		{
				printf("\n ------------------------------ ");
				printf("\n   Roll No: "); fflush(stdin); gets(rollno); strUpper(rollno);
			i=isRollnoExits(rollno);		
		}
		if (i>=0)
			{				
				printf("\n ------------------------------ ");
				printf("\n Full Name: %s",stu[i].name);
				printf("\n  Birthday: %s",stu[i].birth);
				printf("\n   Address: %s",stu[i].add);
				printf("\n     Class: %s",stu[i].classname);
				printf("\n ------------------------------ ");
				fflush(stdin);

				strcpy(stuTemp.rollno,rollno);
				printf("\n Input Full name: "); gets(stuTemp.name);
				printf("\n  Input Birthday: "); gets(stuTemp.birth);
				printf("\n   Input Address: "); gets(stuTemp.add);
				printf("\n     Input Class: "); gets(stuTemp.classname); strUpper(stuTemp.classname);

				printf("\n     Do you want to save (y/n): "); fflush(stdin); scanf("%c",&key);
				if (key=='y')
				{	stu[i] = stuTemp;	}

				printf("\n ------------------------------ ");
				printf("\n Full Name: %s",stu[i].name);
				printf("\n  Birthday: %s",stu[i].birth);
				printf("\n   Address: %s",stu[i].add);
				printf("\n     Class: %s",stu[i].classname);
				printf("\n ------------------------------ ");
			}
		else printf("\n This student dont exit!");
				printf("\n Do you want to continue (y/n): "); fflush(stdin); scanf("%c",&key);
	} while(key!='n');
}

void del_student(int index=-1)
{
	int i,j; char key;
	char rollno[10];

	do
	{	
		printf("\n > Delete Student Information");
		if (index>=0)
		{
			i=index; index=-1;
		}
		else 
		{
				printf("\n ------------------------------ ");
				printf("\n   Roll No: "); fflush(stdin); gets(rollno); strUpper(rollno);
			i=isRollnoExits(rollno);		
		}
		if (i>=0)
			{
				printf("\n ------------------------------ ");
				printf("\n Full Name: %s",stu[i].name);
				printf("\n  Birthday: %s",stu[i].birth);
				printf("\n   Address: %s",stu[i].add);
				printf("\n     Class: %s",stu[i].classname);
				printf("\n ------------------------------ ");
				
				printf("\n   Do you want to delete (y/n): ");fflush(stdin); scanf("%c",&key);
				if (key=='y')
				{
					for(j=i-1;j<scount;j++)
					{				
						strcpy(stu[j].rollno,stu[j+1].rollno);
						strcpy(stu[j].name,stu[j+1].name);
						strcpy(stu[j].birth,stu[j+1].birth);
						strcpy(stu[j].add,stu[j+1].add);
						strcpy(stu[j].classname,stu[j+1].classname);
					}
					scount--;
				}
			}
		else printf("\n This student dont exit!");
				printf("\n Do you want to continue (y/n): "); fflush(stdin); scanf("%c",&key);
	} while(key!='n');	
}

void find_student()
{
	int i; char key;
	char rollno[10];
	do
	{	
		printf("\n > Find Student Information");
				printf("\n ------------------------------ ");
				printf("\n   Roll No: "); fflush(stdin); gets(rollno); strUpper(rollno);

		i=isRollnoExits(rollno);
		if (i>=0)
			{
				printf("\n ------------------------------ ");
				printf("\n Full Name: %s",stu[i].name);
				printf("\n  Birthday: %s",stu[i].birth);
				printf("\n   Address: %s",stu[i].add);
				printf("\n     Class: %s",stu[i].classname);
				printf("\n ------------------------------ ");

				printf("\n Options: ");
				printf("\n \t1. Edit Student Information");
				printf("\n \t2. Delete Student Information");
				printf("\n \t3. Continued to find student");
				printf("\n Select the option (1-3): ");	fflush(stdin); scanf("%d",&key);

				switch(key)
				{
				case 1:edit_student(i);
					break;
				case 2:del_student(i);
					break;
				case 3:
					break;
				default: printf("\n Invalid selection");
				}
         	}
      else printf("\n This student dont exit!");
				printf("\n Do you want to continue (y/n): "); fflush(stdin); scanf("%c",&key);
	} while(key!='n');	
}
           
void o_name(char *str) //HAm lay chu cai dau cua ten
{
	int i,k;
	char ex[100],ho[100],temp;
	for(i=strlen(str);i>=0;i--)	
	{
		if (str[i-1]==' ') 
		{	
			ten[t]=str[i];		
			t++;break;
		}  							
	}
	/*
*/
}
void sapxep()	//sap xep theo ten
{
	int i,k;
	struct_student temp;
	for(i=0;i<scount-1;i++)
		for(k=i+1;k<scount;k++)		
		{			
			if (ten[i]>ten[k])
			{				
				temp=stu[i];
				stu[i]=stu[k];
				stu[k]=temp;			
			}
		}
}

void list_student()
{
	int i,k,j;
	struct_student temp;
	for(i=0;i<scount;i++)
	{	
		o_name(stu[i].name);
		// sapxep();
	}

		printf("\n > Student Information Listing ");
		printf("\n--------------------------------------------------------------------------- ");
		printf("\n %-10s %-30s %-15s %-20s","Roll No","Full Name","Class","Birthday");
	for(i=0;i<scount;i++)
	{
		printf("\n %-10s %-30s %-15s %-20s",stu[i].rollno,stu[i].name,stu[i].classname,stu[i].birth);
		if ((i+1)%10==0)
		{
			printf("\n Press any key to display more information..."); getch(); continue;
		}
	}
		printf("\n--------------------------------------------------------------------------- "); 
		getch();	
}

void info_student()
{
	int key=0;
		do
		{
		system("cls");
			printf("\n Student information");
			printf("\n ------------------------------");
			printf("\n 1. Add New Student");
			printf("\n 2. Edit Student Information");
			printf("\n 3. Delete Student Information");
			printf("\n 4. Student List");
			printf("\n 5. Find Student");
			printf("\n 6. Back to Main Menu");
			printf("\n 7. Exit");
			printf("\n ------------------------------");
			printf("\n Select the option (1-6): ");	fflush(stdin); scanf("%d",&key);

			switch(key) 
			{
				case 1:add_student();
					break;
				case 2:edit_student();
					break;
				case 3:del_student();
					break;
				case 4:list_student();
					break;
				case 5:find_student();
					break;
				case 6:MainMenu();
					break;
				case 7:quit();
					break;
				default: printf("\n Invalid selection");
			}
		} while(1);
		getch();	
}